<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" :labelAlign="'left'">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入计划名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="kaishiriqi">
              <j-date placeholder="请选择计划开始日期" v-model="model.kaishiriqi"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jieshuriqi">
              <j-date placeholder="请选择计划结束日期" v-model="model.jieshuriqi"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="巡检周期（天）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zhouqi">
              <a-input-number v-model="model.zhouqi" placeholder="请输入巡检周期（天）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="计划负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fuzeren">
              <j-select-user-by-dep v-model="model.fuzeren" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beizhu">
              <a-input v-model="model.beizhu" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="8">
            <a-form-model-item label="计划状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zhuangtai">
              <j-dict-select-tag type="list" v-model="model.zhuangtai" dictCode="jihuazhuangtai" placeholder="请选择计划状态" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="点检设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shebei">
              <j-popup
                v-model="model.shebei"
                field="shebei"
                org-fields="device_name,device_id"
                dest-fields="deviceName,shebei"
                code="ene_device"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="点检项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xiangmu">
              <j-popup
                v-model="model.xiangmu_dictText"
                field="xiangmu"
                org-fields="name,id"
                dest-fields="xiangmu_dictText,xiangmu"
                code="gd_dianjian_xiangmu"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'GdDianjianJilhuaForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 23 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 23 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入计划名称!'},
           ],
           kaishiriqi: [
              { required: true, message: '请输入计划开始日期!'},
           ],
           jieshuriqi: [
              { required: true, message: '请输入计划结束日期!'},
           ],
           zhouqi: [
              { required: true, message: '请输入巡检周期（天）!'},
           ],
        },
        url: {
          add: "/gongdan/gdDianjianJilhua/add",
          edit: "/gongdan/gdDianjianJilhua/edit",
          queryById: "/gongdan/gdDianjianJilhua/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>